package com.qf.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.qf.feign.GoodsFeign;
import com.qf.pojo.Goods;
import com.qf.service.TestCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * author:z
 * time:2021/12/18 10:26
 * description:
 * spring中提供了很多的模板对象
 * rabbitmqtemplate
 * jdbctemplate
 * redistemplate
 * stringredis
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    GoodsFeign goodsFeign;

    @Autowired
    EurekaClient eurekaClient;

    @Autowired
    TestCacheService testCacheService;

    @RequestMapping("/testCache")
    public void testCache(){
        System.out.println(testCacheService.testCache(1));
        System.out.println(testCacheService.testCache(1));
        System.out.println(testCacheService.testCache(1));
        testCacheService.clearCache(1);
        System.out.println(testCacheService.testCache(1));
        System.out.println(testCacheService.testCache(1));
        System.out.println(testCacheService.testCache(1));
    }

    @GetMapping("/index")
    public String index(){



        //只能获取一个
//        InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("qf-goods", false);
//        String url = instanceInfo.getHomePageUrl();
//        System.out.println(url);

        //可以根据id获取多个
//        List<InstanceInfo> instances = eurekaClient.getInstancesById("qf-goods");
//        System.out.println(instances);
//        String url=instances.get(0).getHomePageUrl();


        //远程调用商品模块的控制  模仿http请求去调用responseEntity

//        String url="http://qf-goods/index";
        //responseEntity:模拟http的get请求
        //参数1：url路径
        //参数2：响应数据类型
        //参数3：响应实体
//        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url+"/index", String.class);
//        String body = responseEntity.getBody();
//        System.out.println("响应体:"+body);
//        HttpStatus statusCode = responseEntity.getStatusCode();
//        System.out.println(statusCode.is2xxSuccessful()?"成功":"失败");


        String body = goodsFeign.index();

        return "获取订单信息"+body;
    }


    //配置信息
    @HystrixCommand(fallbackMethod = "findByIdFallback", commandProperties ={
            @HystrixProperty(name = "execution.isolation.strategy",value = "THREAD"),
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value ="1000" )
    })
    @GetMapping("/findById")
    public Goods findById(Integer id){
        return  goodsFeign.findById2(id);
    }

    public Goods findByIdFallback(Integer id){
        System.out.println("服务降级了");
        return new Goods(id,"服务降级了");
    }

    @GetMapping("/save")
    public String saveGoods( Goods goods){
        System.out.println("order===================");
        return goodsFeign.save(goods);
    }
}
